<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
</head>

<body>
    <script>
        //抽象工厂方法
        var VehicleFactory = function (subType, superType) {
            if (typeof VehicleFactory[subType] === 'superType') {
                //缓存类
                function F() {};
                //继承父类属性和方法
                F.prototype = new VehicleFactory[superType]();
                //将子类constructor指向子类
                subType.constructor = subType;
                //子类原型继承“父类”
                subType.prototype = new F();
            } else {
                //不村子该抽象类抛出错误
                throw new Error('未创建该抽象类');
            }
        };
        //小汽车抽象类
        VehicleFactory.Car = function () {
            this.type = "car";
        };
        VehicleFactory.Car.prototype = {
            getPrice: function () {
                return new Error('抽象方法不能调用');
            },
            getSpeed: function () {
                return new Error('抽象方法不能调用');
            }
        };
        //公交车抽象类
        VehicleFactory.Bus = function () {
            this.type = "bus";
        };
        VehicleFactory.Bus.prototype = {
            getPrice: function () {
                return new Error('抽象方法不能调用');
            },
            getSpeed: function () {
                return new Error('抽象方法不能调用');
            }
        };
        //货车抽象类
        VehicleFactory.Truck = function () {
            this.type = "truck";
        };
        VehicleFactory.Truck.prototype = {
            getPrice: function () {
                return new Error('抽象方法不能调用');
            },
            getSpeed: function () {
                return new Error('抽象方法不能调用');
            }
        };
        var BMW = function (price, speed) {
            this.price = price;
            this.speed = speed;
        }
        VehicleFactory(BMW, 'Car');
        BMW.prototype.getPrice = function () {
            return this.price;
        }
        BMW.prototype.getSpeed = function () {
            return this.speed;
        }
    </script>
</body>

</html>